package cn.itcast.web.controller.cargo;

import cn.itcast.domain.cargo.*;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * 报运单controller中
 */
@Controller
@RequestMapping("/cargo/export")
public class ExportController extends BaseController {

	@Reference
	private ContractService contractService;

	@Reference
	private ExportService  exportService;

	@Reference
	private ExportProductService exportProductService;


	/**
	 * 合同管理：查询所有的购销合同（条件：状态=1）
	 */
	@RequestMapping("/contractList")
	public String contractList(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size) {
		//1.创建example
		ContractExample example = new ContractExample();
		//2.通过example创建criteria对象
		ContractExample.Criteria criteria = example.createCriteria();
		//3.向criteria对象设置条件
		criteria.andCompanyIdEqualTo(getLoginCompanyId()); //企业id
		criteria.andStateEqualTo(1);
		example.setOrderByClause("create_time DESC");//设置排序
		//4.发起调用
		PageInfo info = contractService.findAll(page, size, example);
		request.setAttribute("page",info);
		//跳转页面
		return "cargo/export/export-contractList";
	}

	/**
	 * 分页查询报运单列表
	 *  条件：企业id （companyId）
	 */
	@RequestMapping("/list")
	public String list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "5")int size) {
		//1.创建example
		ExportExample example = new ExportExample();
		//2.创建criteria
		ExportExample.Criteria criteria = example.createCriteria();
		//3.向criteria设置条件
		criteria.andCompanyIdEqualTo(getLoginCompanyId());
		//4.调用方法查询
		PageInfo info = exportService.findAll(example, page, size);
		request.setAttribute("page",info);
		//跳转页面
		return "cargo/export/export-list";
	}

	/**
	 * 进入到新增报运单页面
	 *  参数：同名参数id   id=1，id=2
	 *      * 接收string数组 （springmvc机制）
	 *      * 接收string字符串（多个id间通过”，“隔开“）
	 *  业务逻辑
	 *      为了在页面上显示id，将id字符串存入request域，跳转页面
	 */
	@RequestMapping("/toExport")
	public String toExport(String id) {
		request.setAttribute("id",id);
		//跳转页面
		return "cargo/export/export-toExport";
	}


	/**
	 * 进入修改页面
	 *  参数:报运单id
	 */
	@RequestMapping("/toUpdate")
	public String toUpdate(String id) {
		//1.根据id查询报运单
		Export export = exportService.findById(id);
		request.setAttribute("export",export);
		//2.根据报运单id查询商品
		ExportProductExample example = new ExportProductExample();
		ExportProductExample.Criteria criteria = example.createCriteria();
		criteria.andExportIdEqualTo(id);
		List list = exportProductService.findAll(example);
		request.setAttribute("eps",list);
		return "cargo/export/export-update";
	}

	/**
	 * 新增或者修改报运单
	 */
	@RequestMapping("/edit")
	public String edit(Export export) {
		//1.设置企业属性
		export.setCompanyId(getLoginCompanyId());
		export.setCompanyName(getLoginCompanyName());
		//2.判断是否具有id
		if (StringUtils.isEmpty(export.getId())) {
			//3.没有id，调用service保存
			exportService.save(export);
		}else {
			//4.存在id，调用service更新
			exportService.update(export);
		}
		//5.重定向到列表
		return  "redirect:/cargo/export/list.do";
	}


	/**
	 * 海关保运方法
	 *  参数：id（报运单id）
	 *  业务：调用service
	 */
	@RequestMapping("/exportE")
	public String exportE(String id) {
		exportService.exportE(id);
		return  "redirect:/cargo/export/list.do";
	}
}
